﻿using System;
using System.Collections.Generic;
using System.Text;
using zyn_hy_login;
using System.Data.SqlClient;
using System.Data;
using DataAccess;
using System.Web;

namespace MOLE.userpkg
{
    public class userx
    {

        public static void AutoLogin()
        {

            System.Web.UI.Page p = (System.Web.UI.Page)HttpContext.Current.Handler;
            z8.Common.Users newus = new z8.Common.Users("womenjia");
            p.Session.Add("yh_info", newus);

            p.Session.Add("yh_info", newus);
            p.Session.Add("aspuid", newus.Id);
            p.Session.Add("aspuname", newus.Name);
            p.Session.Add("usercss", "z8_css1.css");
            p.Session["asputreeid"] = newus.Yh_treeid;
            p.Session["aspugroup"] = newus.Yh_group;
            p.Session["aspuTbname"] = newus.Yh_Tbname;
            p.Session["aspuConn"] = newus.Yh_Connstr;

            p.Session["aspuid"] = "xx";

            p.Session.Add("UserId3str", "");




            p.Response.Cookies["userid"].Value = "861109";
            p.Response.Cookies["username"].Value = "zzzzzz";

            p.Response.Cookies["hy_num"].Value = "861109";
            p.Response.Cookies["hy_name"].Value = "zzzzzz";
            p.Response.Cookies["hy_num"].Expires = DateTime.Now.AddDays(70);

            p.Response.Redirect("/z8/z8_user/zyn_manage.aspx");
        }

        public void login(String uname, string pwd)
        {

            string text = uname;
            if (text.Trim().Equals(""))
            {
                MOLE.global.showMessage("用户名不能为空");
                return;
            }
            //if (((text.Length <= 6) || (text == "")))
            //{
            //    MOLE.global.showMessage("用户名或密码错误");
            //    return;
            //}
            if (pwd.Trim().Equals(""))
            {
                MOLE.global.showMessage("密码不能为空");
                return;
            }
            //if (((pwd.Length <= 6) || (text == "")))
            //{
            //    MOLE.global.showMessage("用户名或密码错误");
            //    return;
            //}


            string	pToEncrypt = new SecurityZ8().Encrypt(pwd);
                            DBCommon.strConnectstring = MOLE.GetValue.GetConfigString("ConnectionString");
							SqlConnection connection = new SqlConnection(DBCommon.strConnectstring);
                            SqlCommand command = new SqlCommand("select top 1 id,UserName,UserPwd,* from Lot_UserInfo where UserName='" + text + "' and UserPwd='" + pToEncrypt + "'  ", connection);
							connection.Open();
							SqlDataReader reader = command.ExecuteReader();
                            if (reader.Read())
                            {
                                string uid = reader.GetValue(1).ToString().Trim();
                                string usname = reader.GetValue(1).ToString().Trim();

                                System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;

                                this.loginok = true;
                                Mypage.Session["hy_num"] = uid;
                                Mypage.Session["hy_name"] = usname;
                                //    base.Response.Cookies.Add(new HttpCookie("hy_name", usname));

                                //     base.Response.Cookies.Add(new HttpCookie("hy_name", usname));

                            }
                            else
                            {
                                MOLE.global.showMessage("用户名或密码错误");
                            }




        }


        public void adduser(String uname, string pwd)
        {

            string text = uname;
            if (text.Trim().Equals(""))
            {
                MOLE.global.showMessage("用户名不能为空");
                return;
            }
           if (((text.Length <= 3) || (text == "")))
           {
               MOLE.global.showMessage("用户名3位以上");
              return;
            }
            if (pwd.Trim().Equals(""))
            {
                MOLE.global.showMessage("密码不能为空");
                return;
            }
           if (((pwd.Length < 6) ))
           {
                MOLE.global.showMessage("密码6位以上");
               return;
            }


         	WWW.SST.LIB.UserV2 o=new WWW.SST.LIB.UserV2();
				o.uname=uname;
				o.upass=pwd;
				o.email="";
				o.cp="";
				o.qq="";
				//o.qq=qq;
				o.Reg();

                if (o.StausMsg.ToLower() == "ok")  //reg ok
                {
                    //labTips.Text=" 注册成功！ ";
                    ok = true;
                 //   if (uc.ok)
                        MOLE.global.showMessage("添加成功");

                }




        }

        public DataTable dt = new DataTable();

        public hy_info getuser(String uname)
        {
            hy_info u = new hy_info();
            string s = "select top 1 id,UserName,UserPwd,* from Lot_UserInfo where UserName='" + uname + "' ";
            DataTable d = MOLE.SQL.GetDataTable(s);
            u.Hy_Name = d.Rows[0]["UserName"].ToString();
            u.Hy_Num = int.Parse( d.Rows[0]["id"].ToString());
            return u;

        }
        public hy_info getCurLoginUser()
        {
                 System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
                 string uname = Mypage.Session["hy_name"].ToString(); ;
                 return getuser(uname);

        }

        public static bool islogin()
        {
            System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
            int i = 0;
            try
            {
             //  
                i = int.Parse(Mypage.Response.Cookies["hy_num"].Value.ToString());
                
                return true;
            }
            catch (Exception e)
            {
                try
                {
                    i = int.Parse(Mypage.Session["hy_num"].ToString());
                    return true;
                }
                catch (Exception e2)
                {
                    return false;
                }
               
                    
            }

        }
        public bool loginok = false;
        public bool ok = false;
    }
}
